//
//  DetailsViewController.m
//  I_LiaoNing
//
//  Created by 王心磊 on 15-4-10.
//  Copyright (c) 2015年 王心磊. All rights reserved.
//

#import "DetailsViewController.h"
#define WIDTH ([UIScreen mainScreen].bounds.size.width/375)
#define HEIGH ([UIScreen mainScreen].bounds.size.height/667)
@interface DetailsViewController ()

@end

@implementation DetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationController.navigationBarHidden=YES;
    self.aView = [[DateView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    [self.view addSubview:self.aView];
   
    NSURL *url = [NSURL URLWithString:self.photo];
     [self.aView.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"999.png"]];
    self.aView.titleLabel.text = self.catename;
   
    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [aButton setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui.png"] forState:UIControlStateNormal];
    [aButton addTarget:self action:@selector(popAction:) forControlEvents:UIControlEventTouchUpInside];
    aButton.frame = CGRectMake(15 * WIDTH, 30 * HEIGH, 40 * WIDTH, 35 * HEIGH);
    [self.view addSubview:aButton];

    self.allCityArray = [NSMutableArray array];
    // Do any additional setup after loading the view.
}

- (void)popAction:(UIButton *)button
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

/*
#pragma mark --下拉刷新
- (void)addHeader
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block ProvinceViewController *ProVc = self;
    
    // 添加下拉刷新头部控件
    [self.tableView addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        //在block块里实现afn数据请求,一定要写在这里
        [ProVc getAFNData:0];
        
        ProVc.isUpLoading = NO;
        NSLog(@"下拉刷新，开始网络请求第一页数据");
        
    }];
#pragma mark  --自动刷新(一进入程序就下拉刷新)
    [self.tableView headerBeginRefreshing];
}


#pragma mark --上拉加载更多
- (void)addFooter
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block ProvinceViewController *vc = self;
    
    // 添加上拉刷新尾部控件
    [self.tableView addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        vc.isUpLoading = YES;
        NSLog(@"上拉加载更多，开始网络请求更多数据");
        
        
        //用afn加载更多的数据
        
        
        self.page++;
        [vc getAFNData:vc.page];
        
        
        
        
    }];
}



#pragma AFN 请求数据

-(void)getAFNData:(NSInteger)page
{
    [GiFHUD show];
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    NSString *url_string = [NSString stringWithFormat:@"http://open.qyer.com/qyer/onroad/poi_list?app_installtime=1428389862&category_id=32&city_id=%@&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&orderby=popular&page=%ld",self.id,page];
    
    
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        
        
        NSDictionary *dic = responseObject;
        NSMutableArray *array = [dic objectForKey:@"data"];
        // 打开数据库
        [[DataHandleDate shareDataBaseHandle]openDB];
        // 清除表
        [[DataHandleDate shareDataBaseHandle]dropTable];
        [[DataHandleDate shareDataBaseHandle ]createDB];
        for (NSDictionary *dic2  in array)
        {
           DetailsModel *model = [[DetailsModel alloc]init];
            [model setValuesForKeysWithDictionary:dic2];
            model.photo = [dic2 objectForKey:@"photo"];
            [self.allCityArray addObject:model];
            [[DataHandleDate shareDataBaseHandle]insertProvinceModel:model];
            
            
        }
        //让自动下拉刷新停止
        [self.tableView headerEndRefreshing];
        [self.tableView footerEndRefreshing];
        [GiFHUD dismiss];
        // 刷新tableView
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //        [GiFHUD dismiss];
        NSLog(@"失败==== %@",error);
        
    }];
    
}

*/






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
